package com.ruoyi.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.service.HotelService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.ElongUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 酒店管理
 * 2024-10-24
 */
@RestController
@RequestMapping("/hotel/hotel")
public class HotelController extends BaseController {

    @Autowired
    HotelService hotelService;

    @Autowired
    ISysUserService iSysUserService;

    /**
     * 获取本地酒店数据
     *
     * @param map
     * @return
     */
    @GetMapping("/getHotelList")
    public TableDataInfo getHotelList(@RequestParam Map map) {
        startPage();
        List<Map> list = hotelService.getHotelList(map);
        return getDataTable(list);
    }

    /**
     * 根据酒店ID获取酒店信息
     *
     * @param hotelId
     * @return
     */
    @GetMapping("/getHotelByHotelId")
    public AjaxResult getHotelByHotelId(@RequestParam String hotelId) {
        return success(hotelService.getHotelByHotelId(hotelId));
    }

    /**
     * 获取艺龙酒店静态数据
     *
     * @param map
     * @return
     */
    @GetMapping("/getELongHotelList")
    public AjaxResult getELongHotelList(@RequestParam Map map) {
        String hotelName = map.get("hotelName").toString();
        String cityId = map.get("cityId[1]").toString();
        JSONArray hotelList = ElongUtils.getHotelList(cityId, hotelName);
        return success(hotelList);
    }

    /**
     * 获取艺龙酒店详细信息
     *
     * @param map
     * @return
     */
    @GetMapping("/getHotelInfo")
    public AjaxResult getHotelInfo(@RequestParam Map map) {
        String eHotelId = map.get("eHotelId").toString();
        return success(hotelService.getHotelInfo(eHotelId));
    }

    /**
     * 本地酒店与艺龙酒店数据同步
     *
     * @param map
     * @return
     */
    @PostMapping("/postEditLocalByOnline")
    public AjaxResult postEditLocalByOnline(@RequestBody Map map) {
        String eHotelId = map.get("eHotelId").toString();
        String hotelId = map.get("hotelId").toString();
        return toAjax(hotelService.editLocalByOnline(eHotelId, hotelId));
    }

    /**
     * 添加酒店
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/postAddHotel")
    public AjaxResult postAddHotel(@RequestBody JSONObject jsonObject) {
        return hotelService.postAddHotel(jsonObject);
    }

    /**
     * 修改酒店
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/postEditHotel")
    public AjaxResult postEditHotel(@RequestBody JSONObject jsonObject) {
        return toAjax(hotelService.postEditHotel(jsonObject));
    }

    /**
     * 酒店下拉列表
     *
     * @return
     */
    @GetMapping("/getHotelListAll")
    public AjaxResult getHotelListAll() {
        List<Map> list = hotelService.getHotelListAll();
        return success(list);
    }

    /**
     * 30分钟免费取消推送
     *
     * @param map
     * @return
     */
    @PostMapping("/postIsFreeCancelable")
    public AjaxResult postIsFreeCancelable(@RequestBody Map map) {
        return toAjax(hotelService.postIsFreeCancelable(map));
    }

    /**
     * 取消规则推送
     *
     * @return
     */
    @PostMapping("/postUpdateRoomRatePlanPolicy")
    public AjaxResult postUpdateRoomRatePlanPolicy(@RequestBody JSONObject jsonObject) {
        return toAjax(hotelService.postUpdateRoomRatePlanPolicy(jsonObject));
    }

    /**
     * 获取运营人员
     *
     * @return
     */
    @GetMapping("/getSysUserInfo")
    public AjaxResult getSysUserInfo() {
        return success(hotelService.getSysUserInfo());
    }

    /**
     * 获取酒店上线信息
     *
     * @param map
     * @return
     */
    @GetMapping("/getOnlineMapping")
    public AjaxResult getOnlineMapping(@RequestParam Map map) {
        String hotelId = map.get("hotelId").toString();
        JSONObject mapping = hotelService.getMapping(hotelId);
        System.out.println(mapping);
        return success(mapping);
    }

    /**
     * 获取酒店所属周末星期
     *
     * @param map
     * @return
     */
    @GetMapping("/getHotelWeekEndByHotelId")
    public AjaxResult getHotelWeekEndByHotelId(@RequestParam Map map) {
        String hotelId = map.get("hotelId").toString();
        return success(hotelService.getHotelWeekEndByHotelId(hotelId));
    }

    /**
     * 修改酒店所属周末星期
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/postEditWeekEnd")
    public AjaxResult postEditWeekEnd(@RequestBody JSONObject jsonObject) {
        return toAjax(hotelService.postEditWeekEnd(jsonObject));
    }

    /**
     * 新增酒店默认周末星期
     */
    @GetMapping("/insertHotelWeekEndDefault")
    public void insertHotelWeekEndDefault() {
        hotelService.insertHotelWeekEndDefault();
    }

    /**
     * 获取酒店发发单文字配置
     *
     * @param map
     * @return
     */
    @GetMapping("/getHotelIssueConfig")
    public AjaxResult getHotelIssueConfig(@RequestParam Map map) {
        String hotelId = map.get("hotelId").toString();
        return success(hotelService.getHotelIssueConfig(hotelId));
    }

    /**
     * 酒店发单模板配置
     *
     * @param jsonObject
     */
    @PostMapping("/postHotelIssueConfig")
    public AjaxResult postHotelIssueConfig(@RequestBody JSONObject jsonObject) {
        return toAjax(hotelService.postHotelIssueConfig(jsonObject));
    }

    /**
     * 酒店露出查询
     *
     * @param map
     * @return
     */
    @GetMapping("/getPriceShowAllList")
    public AjaxResult getPriceShowAllList(@RequestParam Map map) {
        String hotelId = map.get("hotelId").toString();
        Map hotelInfo = hotelService.getHotelByHotelId(hotelId);
        if (hotelInfo.get("eSonHotelId") == null) {
            return AjaxResult.error("缺少关键数据");
        } else {
            String eSonHotelId = hotelInfo.get("eSonHotelId").toString();
            AjaxResult setCurrentHotel = hotelService.setCurrentHotel(eSonHotelId);
            if (setCurrentHotel.isSuccess()) {
                return hotelService.getPriceShowAllList();
            } else {
                return AjaxResult.error();
            }
        }
    }
}
